package com.ythook.mentor.controller;

import com.ythook.mentor.entity.Technologies;
import com.ythook.mentor.service.TechnologiesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Controller
@RequestMapping("/api")
public class TechnologiesController {
    @Resource
    private TechnologiesService technologiesService;
    private Logger logger = LoggerFactory.getLogger(TechnologiesController.class);

    @GetMapping("/technologies")
    public ResponseEntity<List<Technologies>> search() {
        return ResponseEntity.ok(technologiesService.search());
    }

    @GetMapping("/technology/findById/{id}")
    public ResponseEntity<Technologies> search(@PathVariable("id") Long id) {
        return ResponseEntity.ok(technologiesService.findById(id).orElse(new Technologies()));
    }

    @GetMapping("/technology/findByName/{name}")
    public ResponseEntity<List<Technologies>> findByName(@PathVariable("name") String name) {
        return ResponseEntity.ok(technologiesService.findByName(name));
    }

    @DeleteMapping("/technology/delete/{id}")
    public ResponseEntity<Object> delete(@PathVariable("id") Long id){
        technologiesService.delete(id);
        Map<String,Object> map = new HashMap<>();
        map.put("status","success");
        return ResponseEntity.ok(map);
    }

    @PostMapping("/technology/add")
    public ResponseEntity<HttpStatus> add(@RequestBody Technologies technologies){
        technologiesService.add(technologies);
        return ResponseEntity.ok(HttpStatus.CREATED);
    }

    @PutMapping("/technology/update")
    public ResponseEntity<Object> update(@RequestBody Technologies technologies){
        Map<String,Object> map = new HashMap<>();
        map.put("status","success");
        map.put("data", technologiesService.update(technologies));
        return ResponseEntity.ok(map);
    }
}
